library(tidyverse)
## Warning: package 'tidyverse' was built under R version 3.6.3
## -- Attaching packages ------------ tidyverse 1.3.0 --
## v ggplot2 3.3.2     v purrr   0.3.4
## v tibble  3.0.3     v dplyr   1.0.2
## v tidyr   1.1.2     v stringr 1.4.0
## v readr   1.3.1     v forcats 0.4.0
## Warning: package 'ggplot2' was built under R version 3.6.3
## Warning: package 'tibble' was built under R version 3.6.3
## Warning: package 'tidyr' was built under R version 3.6.3
## Warning: package 'purrr' was built under R version 3.6.3
## Warning: package 'dplyr' was built under R version 3.6.3
## -- Conflicts --------------- tidyverse_conflicts() --
## x dplyr::filter() masks stats::filter()
## x dplyr::lag()    masks stats::lag()
Confirmed_State_6_13 <- read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/06-13-2020.csv")) %>%
  filter (Country_Region == "US") %>% 
  group_by(Province_State, Country_Region) %>% 
  summarise(Confirmed = sum(Confirmed)) 
## Parsed with column specification:
## cols(
##   FIPS = col_double(),
##   Admin2 = col_character(),
##   Province_State = col_character(),
##   Country_Region = col_character(),
##   Last_Update = col_datetime(format = ""),
##   Lat = col_double(),
##   Long_ = col_double(),
##   Confirmed = col_double(),
##   Deaths = col_double(),
##   Recovered = col_double(),
##   Active = col_double(),
##   Combined_Key = col_character(),
##   Incidence_Rate = col_double(),
##   `Case-Fatality_Ratio` = col_double()
## )
## `summarise()` regrouping output by 'Province_State' (override with `.groups` argument)
str(Confirmed_State_6_13)
## tibble [58 x 3] (S3: grouped_df/tbl_df/tbl/data.frame)
##  $ Province_State: chr [1:58] "Alabama" "Alaska" "Arizona" "Arkansas" ...
##  $ Country_Region: chr [1:58] "US" "US" "US" "US" ...
##  $ Confirmed     : num [1:58] 24601 653 34660 12095 150018 ...
##  - attr(*, "groups")= tibble [58 x 2] (S3: tbl_df/tbl/data.frame)
##   ..$ Province_State: chr [1:58] "Alabama" "Alaska" "Arizona" "Arkansas" ...
##   ..$ .rows         : list<int> [1:58] 
##   .. ..$ : int 1
##   .. ..$ : int 2
##   .. ..$ : int 3
##   .. ..$ : int 4
##   .. ..$ : int 5
##   .. ..$ : int 6
##   .. ..$ : int 7
##   .. ..$ : int 8
##   .. ..$ : int 9
##   .. ..$ : int 10
##   .. ..$ : int 11
##   .. ..$ : int 12
##   .. ..$ : int 13
##   .. ..$ : int 14
##   .. ..$ : int 15
##   .. ..$ : int 16
##   .. ..$ : int 17
##   .. ..$ : int 18
##   .. ..$ : int 19
##   .. ..$ : int 20
##   .. ..$ : int 21
##   .. ..$ : int 22
##   .. ..$ : int 23
##   .. ..$ : int 24
##   .. ..$ : int 25
##   .. ..$ : int 26
##   .. ..$ : int 27
##   .. ..$ : int 28
##   .. ..$ : int 29
##   .. ..$ : int 30
##   .. ..$ : int 31
##   .. ..$ : int 32
##   .. ..$ : int 33
##   .. ..$ : int 34
##   .. ..$ : int 35
##   .. ..$ : int 36
##   .. ..$ : int 37
##   .. ..$ : int 38
##   .. ..$ : int 39
##   .. ..$ : int 40
##   .. ..$ : int 41
##   .. ..$ : int 42
##   .. ..$ : int 43
##   .. ..$ : int 44
##   .. ..$ : int 45
##   .. ..$ : int 46
##   .. ..$ : int 47
##   .. ..$ : int 48
##   .. ..$ : int 49
##   .. ..$ : int 50
##   .. ..$ : int 51
##   .. ..$ : int 52
##   .. ..$ : int 53
##   .. ..$ : int 54
##   .. ..$ : int 55
##   .. ..$ : int 56
##   .. ..$ : int 57
##   .. ..$ : int 58
##   .. ..@ ptype: int(0) 
##   ..- attr(*, ".drop")= logi TRUE
Confirmed_State_9_13 <-   read_csv(url("https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_daily_reports/09-13-2020.csv")) %>% 
  filter (Country_Region == "US") %>% 
  group_by(Province_State, Country_Region) %>% 
  summarize(Confirmed = sum(Confirmed))
## Parsed with column specification:
## cols(
##   FIPS = col_double(),
##   Admin2 = col_character(),
##   Province_State = col_character(),
##   Country_Region = col_character(),
##   Last_Update = col_character(),
##   Lat = col_double(),
##   Long_ = col_double(),
##   Confirmed = col_double(),
##   Deaths = col_double(),
##   Recovered = col_double(),
##   Active = col_double(),
##   Combined_Key = col_character(),
##   Incidence_Rate = col_double(),
##   `Case-Fatality_Ratio` = col_double()
## )
## `summarise()` regrouping output by 'Province_State' (override with `.groups` argument)
str(Confirmed_State_9_13)
## tibble [58 x 3] (S3: grouped_df/tbl_df/tbl/data.frame)
##  $ Province_State: chr [1:58] "Alabama" "Alaska" "Arizona" "Arkansas" ...
##  $ Country_Region: chr [1:58] "US" "US" "US" "US" ...
##  $ Confirmed     : num [1:58] 138755 6268 208512 70219 761728 ...
##  - attr(*, "groups")= tibble [58 x 2] (S3: tbl_df/tbl/data.frame)
##   ..$ Province_State: chr [1:58] "Alabama" "Alaska" "Arizona" "Arkansas" ...
##   ..$ .rows         : list<int> [1:58] 
##   .. ..$ : int 1
##   .. ..$ : int 2
##   .. ..$ : int 3
##   .. ..$ : int 4
##   .. ..$ : int 5
##   .. ..$ : int 6
##   .. ..$ : int 7
##   .. ..$ : int 8
##   .. ..$ : int 9
##   .. ..$ : int 10
##   .. ..$ : int 11
##   .. ..$ : int 12
##   .. ..$ : int 13
##   .. ..$ : int 14
##   .. ..$ : int 15
##   .. ..$ : int 16
##   .. ..$ : int 17
##   .. ..$ : int 18
##   .. ..$ : int 19
##   .. ..$ : int 20
##   .. ..$ : int 21
##   .. ..$ : int 22
##   .. ..$ : int 23
##   .. ..$ : int 24
##   .. ..$ : int 25
##   .. ..$ : int 26
##   .. ..$ : int 27
##   .. ..$ : int 28
##   .. ..$ : int 29
##   .. ..$ : int 30
##   .. ..$ : int 31
##   .. ..$ : int 32
##   .. ..$ : int 33
##   .. ..$ : int 34
##   .. ..$ : int 35
##   .. ..$ : int 36
##   .. ..$ : int 37
##   .. ..$ : int 38
##   .. ..$ : int 39
##   .. ..$ : int 40
##   .. ..$ : int 41
##   .. ..$ : int 42
##   .. ..$ : int 43
##   .. ..$ : int 44
##   .. ..$ : int 45
##   .. ..$ : int 46
##   .. ..$ : int 47
##   .. ..$ : int 48
##   .. ..$ : int 49
##   .. ..$ : int 50
##   .. ..$ : int 51
##   .. ..$ : int 52
##   .. ..$ : int 53
##   .. ..$ : int 54
##   .. ..$ : int 55
##   .. ..$ : int 56
##   .. ..$ : int 57
##   .. ..$ : int 58
##   .. ..@ ptype: int(0) 
##   ..- attr(*, ".drop")= logi TRUE
setdiff(Confirmed_State_9_13$Province_State, Confirmed_State_6_13$Province_State)
## character(0)
Confirmed_State_9_13 <- Confirmed_State_9_13 %>% 
  filter(Province_State !="Recovered")
Confirmed_State_6_13_9_13_joined <- full_join(Confirmed_State_6_13,
    Confirmed_State_9_13, by = c("Province_State"))
head(Confirmed_State_6_13_9_13_joined)
## # A tibble: 6 x 5
## # Groups:   Province_State [6]
##   Province_State Country_Region.x Confirmed.x Country_Region.y Confirmed.y
##   <chr>          <chr>                  <dbl> <chr>                  <dbl>
## 1 Alabama        US                     24601 US                    138755
## 2 Alaska         US                       653 US                      6268
## 3 Arizona        US                     34660 US                    208512
## 4 Arkansas       US                     12095 US                     70219
## 5 California     US                    150018 US                    761728
## 6 Colorado       US                     29002 US                     61293
tail(Confirmed_State_6_13_9_13_joined, 5)
## # A tibble: 5 x 5
## # Groups:   Province_State [5]
##   Province_State Country_Region.x Confirmed.x Country_Region.y Confirmed.y
##   <chr>          <chr>                  <dbl> <chr>                  <dbl>
## 1 Virginia       US                     53869 US                    133742
## 2 Washington     US                     25538 US                     79826
## 3 West Virginia  US                      2274 US                     12705
## 4 Wisconsin      US                     22518 US                     89185
## 5 Wyoming        US                      1050 US                      4346
Confirmed_State_6_13_9_13_joined <- full_join(Confirmed_State_6_13,
  Confirmed_State_6_13, by = c("Province_State")) %>% 
  rename(Confirmed_6_13_2020 = "Confirmed.x", Confirmed_9_13_2020 = "Confirmed.y") %>%
  select(-Country_Region.x, -Country_Region.y) %>% 
  replace_na(list(Confirmed_6_13_2020 = 0))
head(Confirmed_State_6_13_9_13_joined)
## # A tibble: 6 x 3
## # Groups:   Province_State [6]
##   Province_State Confirmed_6_13_2020 Confirmed_9_13_2020
##   <chr>                        <dbl>               <dbl>
## 1 Alabama                      24601               24601
## 2 Alaska                         653                 653
## 3 Arizona                      34660               34660
## 4 Arkansas                     12095               12095
## 5 California                  150018              150018
## 6 Colorado                     29002               29002
which(is.na(Confirmed_State_6_13_9_13_joined))
## integer(0)

Switching between wide and long table formats

Confirmed_State_6_13_9_13_joined_long <- Confirmed_State_6_13_9_13_joined %>% 
  pivot_longer(-c(Province_State),
               names_to = "Date", values_to = "Confirmed")
Confirmed_joined_long_plot <- ggplot(Confirmed_State_6_13_9_13_joined_long, aes(x = Confirmed, y = Province_State)) +
  geom_col(aes(fill = Date, color = Date))
print(Confirmed_joined_long_plot + ggtitle("Figure 1. Confirmed COVID-19 cases in the US") + labs(y="Province/state", x = "Confirmed cases"))

Working with time series data

Data Wrangling

download.file(url="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv", 
               destfile = "data/time_series_covid19_confirmed_global.csv")
time_series_confirmed <-  read_csv("data/time_series_covid19_confirmed_global.csv")%>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## See spec(...) for full column specifications.
head(time_series_confirmed)
## # A tibble: 6 x 255
##   Province_State Country_Region   Lat   Long `1/22/20` `1/23/20` `1/24/20`
##   <chr>          <chr>          <dbl>  <dbl>     <dbl>     <dbl>     <dbl>
## 1 <NA>           Afghanistan     33.9  67.7          0         0         0
## 2 <NA>           Albania         41.2  20.2          0         0         0
## 3 <NA>           Algeria         28.0   1.66         0         0         0
## 4 <NA>           Andorra         42.5   1.52         0         0         0
## 5 <NA>           Angola         -11.2  17.9          0         0         0
## 6 <NA>           Antigua and B~  17.1 -61.8          0         0         0
## # ... with 248 more variables: `1/25/20` <dbl>, `1/26/20` <dbl>,
## #   `1/27/20` <dbl>, `1/28/20` <dbl>, `1/29/20` <dbl>, `1/30/20` <dbl>,
## #   `1/31/20` <dbl>, `2/1/20` <dbl>, `2/2/20` <dbl>, `2/3/20` <dbl>,
## #   `2/4/20` <dbl>, `2/5/20` <dbl>, `2/6/20` <dbl>, `2/7/20` <dbl>,
## #   `2/8/20` <dbl>, `2/9/20` <dbl>, `2/10/20` <dbl>, `2/11/20` <dbl>,
## #   `2/12/20` <dbl>, `2/13/20` <dbl>, `2/14/20` <dbl>, `2/15/20` <dbl>,
## #   `2/16/20` <dbl>, `2/17/20` <dbl>, `2/18/20` <dbl>, `2/19/20` <dbl>,
## #   `2/20/20` <dbl>, `2/21/20` <dbl>, `2/22/20` <dbl>, `2/23/20` <dbl>,
## #   `2/24/20` <dbl>, `2/25/20` <dbl>, `2/26/20` <dbl>, `2/27/20` <dbl>,
## #   `2/28/20` <dbl>, `2/29/20` <dbl>, `3/1/20` <dbl>, `3/2/20` <dbl>,
## #   `3/3/20` <dbl>, `3/4/20` <dbl>, `3/5/20` <dbl>, `3/6/20` <dbl>,
## #   `3/7/20` <dbl>, `3/8/20` <dbl>, `3/9/20` <dbl>, `3/10/20` <dbl>,
## #   `3/11/20` <dbl>, `3/12/20` <dbl>, `3/13/20` <dbl>, `3/14/20` <dbl>,
## #   `3/15/20` <dbl>, `3/16/20` <dbl>, `3/17/20` <dbl>, `3/18/20` <dbl>,
## #   `3/19/20` <dbl>, `3/20/20` <dbl>, `3/21/20` <dbl>, `3/22/20` <dbl>,
## #   `3/23/20` <dbl>, `3/24/20` <dbl>, `3/25/20` <dbl>, `3/26/20` <dbl>,
## #   `3/27/20` <dbl>, `3/28/20` <dbl>, `3/29/20` <dbl>, `3/30/20` <dbl>,
## #   `3/31/20` <dbl>, `4/1/20` <dbl>, `4/2/20` <dbl>, `4/3/20` <dbl>,
## #   `4/4/20` <dbl>, `4/5/20` <dbl>, `4/6/20` <dbl>, `4/7/20` <dbl>,
## #   `4/8/20` <dbl>, `4/9/20` <dbl>, `4/10/20` <dbl>, `4/11/20` <dbl>,
## #   `4/12/20` <dbl>, `4/13/20` <dbl>, `4/14/20` <dbl>, `4/15/20` <dbl>,
## #   `4/16/20` <dbl>, `4/17/20` <dbl>, `4/18/20` <dbl>, `4/19/20` <dbl>,
## #   `4/20/20` <dbl>, `4/21/20` <dbl>, `4/22/20` <dbl>, `4/23/20` <dbl>,
## #   `4/24/20` <dbl>, `4/25/20` <dbl>, `4/26/20` <dbl>, `4/27/20` <dbl>,
## #   `4/28/20` <dbl>, `4/29/20` <dbl>, `4/30/20` <dbl>, `5/1/20` <dbl>,
## #   `5/2/20` <dbl>, `5/3/20` <dbl>, ...
time_series_confirmed_long <- time_series_confirmed %>%
  pivot_longer(-c(Province_State, Country_Region, Lat, Long),
                names_to = "Date", values_to = "Confirmed")
head(time_series_confirmed_long)
## # A tibble: 6 x 6
##   Province_State Country_Region   Lat  Long Date    Confirmed
##   <chr>          <chr>          <dbl> <dbl> <chr>       <dbl>
## 1 <NA>           Afghanistan     33.9  67.7 1/22/20         0
## 2 <NA>           Afghanistan     33.9  67.7 1/23/20         0
## 3 <NA>           Afghanistan     33.9  67.7 1/24/20         0
## 4 <NA>           Afghanistan     33.9  67.7 1/25/20         0
## 5 <NA>           Afghanistan     33.9  67.7 1/26/20         0
## 6 <NA>           Afghanistan     33.9  67.7 1/27/20         0
download.file(url="https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_deaths_global.csv", 
               destfile = "data/time_series_covid19_deaths_global.csv")
time_series_deaths <- read_csv("data/time_series_covid19_deaths_global.csv") %>%
  rename(Province_State = "Province/State", Country_Region = "Country/Region")
## Parsed with column specification:
## cols(
##   .default = col_double(),
##   `Province/State` = col_character(),
##   `Country/Region` = col_character()
## )
## See spec(...) for full column specifications.
time_series_deaths_long <- time_series_deaths %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long),
                  names_to = "Date", values_to = "Deaths")
head(time_series_deaths_long)
## # A tibble: 6 x 6
##   Province_State Country_Region   Lat  Long Date    Deaths
##   <chr>          <chr>          <dbl> <dbl> <chr>    <dbl>
## 1 <NA>           Afghanistan     33.9  67.7 1/22/20      0
## 2 <NA>           Afghanistan     33.9  67.7 1/23/20      0
## 3 <NA>           Afghanistan     33.9  67.7 1/24/20      0
## 4 <NA>           Afghanistan     33.9  67.7 1/25/20      0
## 5 <NA>           Afghanistan     33.9  67.7 1/26/20      0
## 6 <NA>           Afghanistan     33.9  67.7 1/27/20      0

Exercise 4

time_series_deaths_long_plot <- time_series_deaths_long %>% 
 group_by(Country_Region) %>% 
  summarise(Deaths = sum(Deaths)) %>%
  ggplot(aes(x = Date, y = Deaths)) +
  geom_line() +
  ggtitle("Confirmed COVID-19 deaths worldwide")
## `summarise()` ungrouping output (override with `.groups` argument)

Joining the time series tables

time_series_confirmed_long <- time_series_confirmed_long %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".", remove = FALSE)
head(time_series_confirmed_long)
## # A tibble: 6 x 7
##   Key            Province_State Country_Region   Lat  Long Date   Confirmed
##   <chr>          <chr>          <chr>          <dbl> <dbl> <chr>      <dbl>
## 1 NA.Afghanista~ <NA>           Afghanistan     33.9  67.7 1/22/~         0
## 2 NA.Afghanista~ <NA>           Afghanistan     33.9  67.7 1/23/~         0
## 3 NA.Afghanista~ <NA>           Afghanistan     33.9  67.7 1/24/~         0
## 4 NA.Afghanista~ <NA>           Afghanistan     33.9  67.7 1/25/~         0
## 5 NA.Afghanista~ <NA>           Afghanistan     33.9  67.7 1/26/~         0
## 6 NA.Afghanista~ <NA>           Afghanistan     33.9  67.7 1/27/~         0
time_series_deaths_long <- time_series_deaths_long %>% 
  unite(Key, Province_State, Country_Region, Date, sep = ".") %>% 
  select(Key, Deaths)
time_series_long_joined <- full_join(time_series_confirmed_long, time_series_deaths_long, by = c("Key")) %>% 
  select(-Key)
head(time_series_long_joined)
## # A tibble: 6 x 7
##   Province_State Country_Region   Lat  Long Date    Confirmed Deaths
##   <chr>          <chr>          <dbl> <dbl> <chr>       <dbl>  <dbl>
## 1 <NA>           Afghanistan     33.9  67.7 1/22/20         0      0
## 2 <NA>           Afghanistan     33.9  67.7 1/23/20         0      0
## 3 <NA>           Afghanistan     33.9  67.7 1/24/20         0      0
## 4 <NA>           Afghanistan     33.9  67.7 1/25/20         0      0
## 5 <NA>           Afghanistan     33.9  67.7 1/26/20         0      0
## 6 <NA>           Afghanistan     33.9  67.7 1/27/20         0      0
which(is.na(time_series_long_joined$Confirmed))
## integer(0)
which(is.na(time_series_long_joined$Deaths))
## integer(0)

Exercise 5

time_series_joined_ex5 <- time_series_long_joined %>% 
  filter (Country_Region == "US") %>% 
  drop_na(Deaths, Confirmed) %>% 
  mutate(Deaths_confirmed = Deaths / Confirmed)
str(time_series_joined_ex5)
## tibble [251 x 8] (S3: tbl_df/tbl/data.frame)
##  $ Province_State  : chr [1:251] NA NA NA NA ...
##  $ Country_Region  : chr [1:251] "US" "US" "US" "US" ...
##  $ Lat             : num [1:251] 40 40 40 40 40 40 40 40 40 40 ...
##  $ Long            : num [1:251] -100 -100 -100 -100 -100 -100 -100 -100 -100 -100 ...
##  $ Date            : chr [1:251] "1/22/20" "1/23/20" "1/24/20" "1/25/20" ...
##  $ Confirmed       : num [1:251] 1 1 2 2 5 5 5 6 6 8 ...
##  $ Deaths          : num [1:251] 0 0 0 0 0 0 0 0 0 0 ...
##  $ Deaths_confirmed: num [1:251] 0 0 0 0 0 0 0 0 0 0 ...

Exercise 6

time_series_joined_ex5 %>% 
  group_by(Country_Region,Date) %>% 
  filter (Country_Region == "US") %>% 
  ggplot(aes(x = Date, y = Deaths_confirmed))+
  geom_point() +
  geom_line() +
  ggtitle("US COVID-19 deaths/confirmed cases per day")
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?

library(lubridate)
## Warning: package 'lubridate' was built under R version 3.6.3
## 
## Attaching package: 'lubridate'
## The following objects are masked from 'package:base':
## 
##     date, intersect, setdiff, union
time_series_long_joined$Date <- mdy(time_series_long_joined$Date)

Exercise 7

library(DT)
## Warning: package 'DT' was built under R version 3.6.3
time_series_long_joined %>% 
  group_by(Country_Region) %>% 
  summarize(Deaths = sum(Deaths, na.rm = TRUE)) %>% 
  slice_max(Deaths, n = 10) %>% 
  arrange(desc(Deaths))
## `summarise()` ungrouping output (override with `.groups` argument)
## # A tibble: 10 x 2
##    Country_Region   Deaths
##    <chr>             <dbl>
##  1 US             22505112
##  2 Brazil         11363329
##  3 United Kingdom  6408784
##  4 Italy           5941499
##  5 Mexico          5656827
##  6 India           5115949
##  7 France          4983267
##  8 Spain           4881010
##  9 Iran            2353899
## 10 Peru            2345552
time_series_long_joined %>% 
  filter(Country_Region %in% c("US", "Brazil", "United Kingdom", "Italy", "Mexico", "France", "Spain", "India", "Iran", "Peru")) %>%
  ggplot(aes(x = Date, y = Deaths, color = Country_Region)) +
  geom_point() +
  geom_line() +
  ggtitle("Top 10 Countries with COVID-19-related Deaths")

Exercise 8

time_series_long_joined %>% 
 group_by(Country_Region,Date) %>% 
  summarise_at(c("Deaths"), sum) %>%
  filter (Country_Region %in% c("US", "Brazil", "United Kingdom", "Italy", "Mexico", "France", "Spain", "India", "Iran", "Peru")) %>% 
  ggplot(aes(x = Date, y = Deaths)) +
  geom_point() +
  geom_line() +
  ggtitle("COVID-19 Deaths") +
  facet_wrap(~Country_Region, ncol=2, scales="free_y") +
  theme_dark()

Exercise 9

time_series_joined_ex5 %>% 
 group_by(Province_State,Date) %>% 
  summarise_at(c("Confirmed"), sum) %>%
  ggplot(aes(x = Date, y = Confirmed)) +
  geom_point() +
  geom_line() +
  ggtitle("Confirmed COVID-19 cases in the US") +
  facet_wrap(~Province_State, ncol=25, scales="free_y")
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?

time_series_joined_ex5 %>% 
    ggplot(aes(x = Date, y = Confirmed), size = 0.5) +
    geom_point() +
    geom_line() +
    ggtitle("Confirmed COVID-19 cases in the US") +
    facet_wrap(~Province_State, ncol=5, scales="free_y")
## geom_path: Each group consists of only one observation. Do you need to
## adjust the group aesthetic?

time_series_long_joined_counts <- time_series_long_joined %>% 
  pivot_longer(-c(Province_State, Country_Region, Lat, Long, Date),
               names_to = "Report_Type", values_to = "Counts")
head(time_series_long_joined_counts)
## # A tibble: 6 x 7
##   Province_State Country_Region   Lat  Long Date       Report_Type Counts
##   <chr>          <chr>          <dbl> <dbl> <date>     <chr>        <dbl>
## 1 <NA>           Afghanistan     33.9  67.7 2020-01-22 Confirmed        0
## 2 <NA>           Afghanistan     33.9  67.7 2020-01-22 Deaths           0
## 3 <NA>           Afghanistan     33.9  67.7 2020-01-23 Confirmed        0
## 4 <NA>           Afghanistan     33.9  67.7 2020-01-23 Deaths           0
## 5 <NA>           Afghanistan     33.9  67.7 2020-01-24 Confirmed        0
## 6 <NA>           Afghanistan     33.9  67.7 2020-01-24 Deaths           0

Making Graphs from time series data

time_series_long_joined %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
  filter (Country_Region == "US") %>% 
  ggplot(aes(x = Date, y = Deaths))+
  geom_point() +
  geom_line() +
  ggtitle("US COVID-19 Deaths")

time_series_long_joined %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>%
  filter (Country_Region %in% c("China","Japan", "Korea, South","Italy","Spain","US")) %>% 
  ggplot(aes(x = Date, y = Deaths)) +
  geom_point() +
  geom_line() +
  ggtitle("COVID-19 Deaths") +
  facet_wrap(~Country_Region, ncol=2, scales="free_y")

time_series_long_joined %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>% 
  filter (Country_Region %in% c("China","France","Italy","Korea, South","US")) %>% 
  ggplot(aes(x = Date, y = Deaths, color = Country_Region)) +
  geom_point() +
  geom_line() +
  ggtitle("COVID-19 Deaths")

time_series_long_joined_counts %>% 
  group_by(Country_Region, Report_Type, Date) %>% 
  summarise(Counts = sum(Counts)) %>% 
  filter (Country_Region == "US") %>% 
  ggplot(aes(x = Date, y = log2(Counts), fill = Report_Type, color = Report_Type)) +
           geom_point() +
           geom_line() +
           ggtitle("US COVID-19 Cases")
## `summarise()` regrouping output by 'Country_Region', 'Report_Type' (override with `.groups` argument)

Supplemental Lab 5 information

Graphic output

pdf("file_name.pdf", width=6, height=3)
time_series_long_joined %>% 
  group_by(Country_Region,Date) %>%
  summarise_at(c("Confirmed", "Deaths"), sum) %>%
  filter (Country_Region == "US") %>% 
    ggplot (aes(x = Date, y = Deaths)) +
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Deaths")
dev.off()
## png 
##   2
ppi <- 300
png("time_series_ex5_1.png", width=6*ppi, height=6*ppi, res=ppi)
time_series_long_joined %>% 
  group_by(Country_Region,Date) %>% 
  summarise_at(c("Confirmed", "Deaths"), sum) %>%
  filter (Country_Region == "US") %>% 
    ggplot(aes(x = Date, y = Deaths)) +
    geom_point() +
    geom_line() +
    ggtitle("US COVID-19 Deaths")
dev.off()
## png 
##   2

RMarkdown loading images

US COVID-19 Deaths

Interactive graphs

library(plotly)
## Warning: package 'plotly' was built under R version 3.6.3
## 
## Attaching package: 'plotly'
## The following object is masked from 'package:ggplot2':
## 
##     last_plot
## The following object is masked from 'package:stats':
## 
##     filter
## The following object is masked from 'package:graphics':
## 
##     layout
US_deaths <- time_series_long_joined %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region == "US")
 p <- ggplot(data = US_deaths, aes(x = Date,  y = Deaths)) + 
        geom_point() +
        geom_line() +
        ggtitle("US COVID-19 Deaths")
ggplotly(p)

Animated graphs with gganimate

library(gganimate)
## Warning: package 'gganimate' was built under R version 3.6.3
## No renderer backend detected. gganimate will default to writing frames to separate files
## Consider installing:
## - the `gifski` package for gif output
## - the `av` package for video output
## and restarting the R session
library(transformr)
## Warning: package 'transformr' was built under R version 3.6.3
theme_set(theme_minimal())
data_time <- time_series_long_joined %>% 
    group_by(Country_Region,Date) %>% 
    summarise_at(c("Confirmed", "Deaths"), sum) %>% 
    filter (Country_Region %in% c("China","Korea, South","Japan","Italy","US")) 
p <- ggplot(data_time, aes(x = Date,  y = Confirmed, color = Country_Region)) + 
      geom_point() +
      geom_line() +
      ggtitle("Confirmed COVID-19 Cases") +
      geom_point(aes(group = seq_along(Date))) +
      transition_reveal(Date) 
animate(p, end_pause = 15)
## Warning: No renderer available. Please install the gifski, av, or magick
## package to create animated output